package com.test.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.test.bean.User;
import com.test.dao.UserDAO;
import com.test.service.UserService;
import com.test.util.RandomStringUtil;

public class UserServiceImpl implements UserService
{

	private UserDAO userDAO;

	public UserDAO getUserDAO()
	{
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO)
	{
		this.userDAO = userDAO;
	}

	public void delete(User user)
	{
		this.userDAO.remove(user);
	}

	public List<User> findAll()
	{
		return this.userDAO.findAllUsers();
	}

	public User findById(Integer id)
	{
		return this.userDAO.findUserByID(id);
	}

	public void save(User user)
	{
		this.userDAO.saveUser(user);
	}

	public void update(User user)
	{
		this.userDAO.updateUser(user);
	}

	public InputStream getInputStream()
	{
		List<User> list = this.userDAO.findAllUsers();
		// 建立工作薄和工作表
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet s = wb.createSheet();
		wb.setSheetName(0, "user");// sheet名不能是中文.
		// 初始化
		HSSFRow row = null;
		HSSFCell cell = null;
		int i;
		short j;
		// 表头
		i = 0;
		row = s.createRow(i++);
		j = 0;
		cell = row.createCell(j++);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("ID");

		cell = row.createCell(j++);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("姓");

		cell = row.createCell(j++);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("名");

		cell = row.createCell(j++);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("年龄");

		// 表格内容
		i = 1;
		for (User user : list)
		{
			j = 0;
			row = s.createRow(i++);

			cell = row.createCell(j++);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(user.getId());

			cell = row.createCell(j++);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(user.getLastname());

			cell = row.createCell(j++);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(user.getFirstname());

			cell = row.createCell(j++);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(user.getAge());
		}
		
		//方法3
		ByteArrayOutputStream os=new ByteArrayOutputStream();
		try
		{
			wb.write(os);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		byte[] bytes=os.toByteArray();
		InputStream is = new ByteArrayInputStream(bytes);
		
		return is;
		
//		//方法2//不能用
//		byte[] bytes=wb.getBytes();
//		InputStream is = new ByteArrayInputStream(bytes);
//		return is;
		
//		//方法1
//		// 输出文件
//		// String string =RandomStringUtil.getRandomString(10);//这个是自己写的工具类
//		String string = RandomStringUtils.randomAlphanumeric(10);// 这个是commons-lang的工具类
//		final File file = new File(string + ".xls");
//
//		try
//		{
//			OutputStream os = new FileOutputStream(file);
//			wb.write(os);
//			os.close();
//
//		} catch (Exception e1)
//		{
//			e1.printStackTrace();
//		}
//
//		// 输入流
//		InputStream is = null;
//		try
//		{
//			is = new FileInputStream(file);
//		} catch (FileNotFoundException e)
//		{
//			e.printStackTrace();
//		}
//
//		new Thread(new Runnable()
//		{
//			public void run()
//			{
//				try
//				{
//					Thread.sleep(10000);
//				} catch (InterruptedException e)
//				{
//					e.printStackTrace();
//				}
//				file.delete();
//			}
//		}).start();
//
//		return is;
	}
}
